﻿@inject ModalService ModalService

<Input @bind-Value="@value" />
<br />
<br />
<Button OnClick="OpenComponent" Type="ButtonType.Primary">Use Component</Button>
<Button OnClick="OpenTemplate" Type="ButtonType.Primary">Use Template</Button>

@code {

    private string value = "blazor";

    private ConfirmRef _confirmRef;

    private async Task OpenComponent()
    {
        RenderFragment content =@<div>value: @value <br /><Button OnClick="CloseModal">close</Button></div>;

        var options = new ConfirmOptions<string>()
        {
            Title = "Confirm",
            Width = 350,
            Content = content,
            OnOpen = () =>
            {
                Console.WriteLine("Open Confirm");
                return Task.CompletedTask;
            },
            OnClose = () =>
            {
                Console.WriteLine("Close Confirm");
                return Task.CompletedTask;
            },
            OnCancel = (result) =>
            {
                Console.WriteLine($"OnCancel:{result}");
                return Task.CompletedTask;
            },
            OnOk = (result) =>
            {
                Console.WriteLine($"OnOk:{result}");
                return Task.CompletedTask;
            }
        };

        await ModalService.CreateConfirmAsync(options);
    }

    private async Task CloseModal()
    {
        if (_confirmRef != null)
        {
            await _confirmRef.CloseAsync();
        }
    }

    private async Task OpenTemplate()
    {
        var options = new ConfirmOptions()
        {
            Title = "Component",
        };

        await ModalService.CreateConfirmAsync<ConfirmTemplateDemo, string, string>(options, value);
    }
}